package com.lungle.warmdiary.app.view.wallet.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseFragment;
import com.lungle.warmdiary.app.base.BaseSubscriber;
import com.lungle.warmdiary.app.model.Result;
import com.lungle.warmdiary.app.model.request.WithdrawPost;
import com.lungle.warmdiary.app.model.response.AlipayAccount;
import com.lungle.warmdiary.app.ui.dialog.CommonDialog;
import com.umeng.analytics.MobclickAgent;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by God on 2016/8/14.
 */
public class WithdrawCenterFragment extends BaseFragment {
    private static final String WITHDRAW_CENTER_SCREEN = "withdraw center screen";

    private EditText mEtBalance;
    private Button mBtnTakecashAll;
    private CheckBox mCbAccountSelect;
    private TextView mTvAccount;
    private Button mBtnSubmit;
    private Button mBtnRule;
    private AlipayAccount mAlipay;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(getLayoutId(), container, false);


        initData();
        initView(view);
        return view;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_takecash_center;
    }

    @Override
    public void initView(View view) {
        mEtBalance = (EditText) view.findViewById(R.id.et_balance);
        mBtnTakecashAll = (Button) view.findViewById(R.id.btn_takecash_all);
        mCbAccountSelect = (CheckBox) view.findViewById(R.id.cb_account_select);
        mTvAccount = (TextView) view.findViewById(R.id.tv_account);
        mBtnSubmit = (Button) view.findViewById(R.id.btn_submit);
        mBtnRule = (Button) view.findViewById(R.id.btn_takecash_rule);
        mBtnRule.setVisibility(View.GONE);

        mBtnSubmit.setOnClickListener(this);
        mBtnRule.setOnClickListener(this);
        mBtnTakecashAll.setOnClickListener(this);


        mEtBalance.setHint("最多提现 " + mAlipay.getIncome() + "元");
        if (mAlipay.getAccount().length() > 5) {
            int i = mAlipay.getAccount().length() - 4;
            mTvAccount.setText(mAlipay.getAccount().substring(0, i) + "****");

        } else {
            mTvAccount.setText(mAlipay.getAccount());
        }


    }

    @Override
    public void initData() {
        mAlipay = (AlipayAccount) getArguments().getSerializable("alipay");
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_takecash_all:
                mEtBalance.setText(mAlipay.getIncome() + "");
                break;
            case R.id.btn_takecash_rule:
                break;
            case R.id.btn_submit:
                String text = mEtBalance.getText().toString().trim();
                if (TextUtils.isEmpty(text)) {
                    AppContext.showToast("请输入金额");
                    return;
                }

                double balance = Double.parseDouble(text);
                if (checkAmount(balance) && checkAccount()) submit(balance);
                break;
        }
    }

    private void submit(double balance) {
        showWaitDialog("申请提现");
        WithdrawPost post = new WithdrawPost(new WithdrawPost.Post(mAlipay.getId(), balance));

        ApiFactory.getUserApi().withdraw(post).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<Result>(getActivity()) {
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        hideWaitDialog();
                        AppContext.showToast(e.getMessage());
                    }

                    @Override
                    public void onNext(Result result) {
                        super.onNext(result);

                    }

                    @Override
                    public void onCompleted() {
                        hideWaitDialog();


                        CommonDialog dialog = new CommonDialog(getActivity());
                        dialog.setMessage(getResources().getString(R.string.dialog_take_cash));
                        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                dialog.dismiss();
                                getActivity().finish();
                            }
                        });
                        dialog.show();
                    }
                });


    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(WITHDRAW_CENTER_SCREEN);
        MobclickAgent.onResume(getActivity());

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(WITHDRAW_CENTER_SCREEN);
        MobclickAgent.onPause(getActivity());

    }


    private boolean checkAmount(double balance) {
        if (balance < 100) {

            AppContext.showToast("最低提现100");
            return false;
        }
        if (balance <= mAlipay.getIncome()) {
            return true;
        } else {
            AppContext.showToast("金额输入错误");
            return false;
        }
    }

    private boolean checkAccount() {
        if (!mCbAccountSelect.isChecked()) {
            AppContext.showToast("选择账户");
            return false;
        }
        return true;
    }


}
